package pe.edu.upc.qwerty.movil;

import java.io.IOException;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class VuelosActivity extends Activity {
    /** Called when the activity is first created. */
	
	private static final String METHOD_NAME = "validarUsuario";
	public static String USUARIO_LOGIN = "";
	public static String PASS_LOGIN = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void loginHandler(View view) {
    	  Log.d("create","aki prueba de log");
    	String usuario="", password="" ; 
    	
		switch (view.getId()) {
		case R.id.btnLogin:
				usuario = ((EditText) findViewById(R.id.txtUser)).getText().toString();
				password = ((EditText) findViewById(R.id.txtPassword)).getText().toString();				
		    	//System.out.println(usuario);
		    	//System.out.println(password);
		    	  Log.d("LOGIN"," usuario  "+ usuario);
			//	llamaServicio();
					if(validarUsuario(usuario, password))
					{
						Intent intent = new Intent(VuelosActivity.this, Lista.class);
			     		startActivity(intent);
			     		USUARIO_LOGIN = usuario;
			     		PASS_LOGIN = password;
					}
					else
					{
						AlertDialog alertDialog = new AlertDialog.Builder(this).create();
						alertDialog.setTitle("Error");
						alertDialog.setMessage("Usuario y password incorecto, porfavor vuelva a intentarlo");
						alertDialog.setButton("Cerrar", new DialogInterface.OnClickListener() {
						   public void onClick(DialogInterface dialog, int which) {
						      
						   }
						});
						alertDialog.setIcon(R.drawable.icon);
						alertDialog.show();
					}
				
			break;
		}
	}
    
    private boolean validarUsuario(String usuario, String password)
    {
    	SoapObject request = new SoapObject(Lista.NAMESPACE, METHOD_NAME);
    	request.addProperty("usuario", usuario);
    	request.addProperty("password", password);
    	
    	
    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(Lista.URL,Lista.TIMEOUT);
        SoapObject respuesta = null;
     	try {
			androidHttpTransport.call( Lista.NAMESPACE+METHOD_NAME , envelope);
			respuesta = (SoapObject)envelope.getResponse();
			//respuesta.get
			int codigoRespuesta = Integer.parseInt(respuesta.getPropertyAsString("codigo"));
			return codigoRespuesta == 0;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
    	return false;
    }
    
    
}